//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    grade:0,
    ifdisable:true,
    orderinfo:{
      packDate:'',
      billOfLading:'',
      boxNum:'',
      carNo:'',
      driverPhone:'',
      boxtype:'',
      tsphone:''
    }
  },
  onLoad: function (options) {
    // this.getorderinfofunc('carNo', {
    //   carNo: '浙B8U218',
    //   phone: '13186850898'
    // })
    // return false;
    // this.getorderinfofunc('orderid', {id: 798})
    // return false;
    var data = {}
    if (options.plateno) {
      data = {
        carNo: options.plateno,
        phone: app.globalData.phone
      }
      //根据车牌打分
      this.getorderinfofunc('carNo', data)
    } else {
      data = {
        id: options.orderid
      }
      this.getorderinfofunc('orderid', data)
    }
    wx.showShareMenu({
      withShareTicket: true
    })
    
  },
  submit(){
    var that = this;
    wx.request({
      url: app.globalData.evaluateurl + 'apint/bill/insertWkScore',
      data: {
        score: that.data.grade,
        relorderid: that.data.orderinfo.orderid,
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'post',
      success: function (res) {
        if(res.data.result == 'success'){
          that.setData({
            ifdisable: true
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  gpsFunc(){
    if (this.data.orderinfo.carNo!=''){
      if(this.data.orderinfo.boxNum!=''){
        wx.navigateTo({
          url: '../map/map?vehicleno=' + this.data.orderinfo.carNo,
        })
      }else{
        wx.showToast({
          title: '还未提箱，暂无GPS信息',
          icon: 'none',
          duration: 2000
        })
      }
    }else{
      wx.showToast({
        title: '未获取到车牌号，请扫码后再试',
        icon: 'none',
        duration: 2000
      })
    }
    
  },
  ylrFunc(){
    wx.showToast({
      title: '研发中，未开放',
      icon: 'none',
      duration: 2000
    })
  },
  callkf() {
    var that = this;
    wx.showModal({
      title: '联系客服',
      content: '拨打电话：' + that.data.orderinfo.tsphone,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: that.data.orderinfo.tsphone,
          })
        }
      },
    })
  },
  shareFunc(){
    wx.updateShareMenu({
      withShareTicket: true,
      success() { }
    })
  },
  copyFunc(){
    var data = `做箱时间： ${this.data.orderinfo.packDate}
提单号：${this.data.orderinfo.billOfLading }
箱型：${this.data.orderinfo.boxtype}
箱号：${this.data.orderinfo.boxNum}
封号：${this.data.orderinfo.sealno}
船名：${this.data.orderinfo.vessel}
航次：${this.data.orderinfo.voyage}
箱门重量：${this.data.orderinfo.weight}
车牌号：${this.data.orderinfo.carNo}
司机电话：${this.data.orderinfo.driverPhone}
装箱地址：${this.data.orderinfo.factoryAddress}`
    wx.setClipboardData({
      data: data,
      success: function (res) {
      }
    })
  },
  getgrade(grade){
    this.setData({
      grade:grade.detail
    })
  },
  getorderinfofunc(type,data){
    let url = ''
    if(type == 'orderid'){
      url = 'apint/bill/selectWkOrderOne'
    }else{
      url = 'apint/bill/order/selectWkOrderFirst'
    }
    
    var that = this;
    wx.request({
      url: app.globalData.evaluateurl + url,
      data: data,
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'post',
      success: function (res) {
        const retdata = res.data[0]
        
        if (retdata!=null) {
          that.setData({
            orderinfo: retdata
          })
          
          if (retdata.driverPhone != app.globalData.phone) {
            if (retdata.score){
              that.setData({
                ifdisable: true,
                grade: retdata.score
              })
            }else{
              that.setData({
                ifdisable: true,
              })
            }
          } else {
            if (retdata.score) {
              that.setData({
                ifdisable: true,
                grade: retdata.score
              })
            } else {
              that.setData({
                ifdisable: false,
              })
            }
          }
        }else{
          that.setData({
            ifdisable: true
          })
        }
        
      }
    })
  },
  dialdriver(){
    var that = this;
    wx.showModal({
      title: '联系司机',
      content: '拨打电话：' + that.data.orderinfo.driverPhone,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: that.data.orderinfo.driverPhone,
          })
        }
      },
    })
  }
})
